package sockettest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 
 * @author ZZWang
 * @Time 2017年9月14日  上午11:10:50
 */
public class SocketClient {

	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket socket = new Socket("localhost",8080);
		// 发送消息
		OutputStream output = socket.getOutputStream();
		OutputStreamWriter writer = new OutputStreamWriter(output,"utf-8");
		BufferedWriter bufferWri = new BufferedWriter(writer);
		bufferWri.write("我叫小明，今年20岁！");
		bufferWri.flush();
		socket.shutdownOutput();
		// 接收消息
		InputStream input = socket.getInputStream();
		InputStreamReader reader = new InputStreamReader(input, "utf-8");
		BufferedReader bufferRea = new BufferedReader(reader);
		String data;
		while ((data = bufferRea.readLine()) != null) {
			System.out.println("我是客户端，服务器说：" + data);
		}
		
		bufferWri.close();
		writer.close();
		output.close();
		bufferRea.close();
		reader.close();
		input.close();
		socket.close();
	}

}
